#!/usr/bin/make -f
# This file has gone through many maintainers.  Mostly rewritten
# by Daniel Jacobowitz.

# This is the debhelper compatability version to use.
export DH_COMPAT=3

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are important since this is a library package
soname=5
vername=5.7
sodepver = (>= 5.7-1)

# Name our packages
package-base=ncurses-base
package-bin=ncurses-bin
package-lib=libncurses$(soname)
package-lib-64=lib64ncurses$(soname)
package-libw=libncursesw$(soname)
package-dev=libncurses$(soname)-dev
package-dev-64=lib64ncurses$(soname)-dev
package-devw=libncursesw$(soname)-dev
package-dbg=libncurses$(soname)-dbg
package-dbgw=libncursesw$(soname)-dbg
package-term=ncurses-term

workdir=$(shell pwd)
tempdir=debian/tmp
fulltempdir=$(workdir)/$(tempdir)

srcdir=$(shell pwd)
objdir=$(srcdir)/obj
objdir-static=$(srcdir)/obj-static
objdir-64=$(srcdir)/obj-64
wobjdir=$(srcdir)/obj-wide
objdir-debug=$(srcdir)/obj-debug
wobjdir-debug=$(srcdir)/obj-wide-debug

DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_HOST_GNU_TYPE),s390-linux)
build_64_target = s390x-linux
build_64 = build-64
endif

ifeq ($(DEB_HOST_GNU_TYPE),sparc-linux)
build_64_target = sparc64-linux
build_64 = build-64
endif

CFLAGS = -O2 -g

ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif

CONFARGS =	--prefix=/usr \
		--with-shared \
		--mandir='$${datadir}/man' \
		--without-profile --without-debug \
		--disable-rpath --enable-echo \
		--enable-const \
		--without-ada \
		--disable-termcap --with-terminfo-dirs="/etc/terminfo:/usr/share/terminfo"

# Directories and files for /etc/terminfo
etcterminfodirs=E a c d l m p r s v w x
etcterminfofiles=a/ansi c/cons25 c/cygwin d/dumb l/linux m/mach m/mach-bold \
	m/mach-color p/pcansi r/rxvt r/rxvt-m r/rxvt-basic s/screen \
	s/screen-bce s/screen-s s/screen-w s/sun v/vt100 v/vt102 \
	v/vt220 v/vt52 x/xterm x/xterm-debian x/xterm-xfree86 x/xterm-color \
	x/xterm-mono x/xterm-r5 x/xterm-r6 x/xterm-vt220 \
	w/wsvt25 w/wsvt25m E/Eterm E/Eterm-color

patch:
	env QUILT_PATCHES=$(CURDIR)/debian/patches quilt push -a
	touch $@

$(objdir)/config.status: patch
	test -d $(objdir) || mkdir $(objdir)
	cd $(objdir) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite

$(objdir-static)/config.status: patch
	test -d $(objdir-static) || mkdir $(objdir-static)
	cd $(objdir-static) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		--without-shared --enable-static \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite

$(objdir-64)/config.status: patch
	test -d $(objdir-64) || mkdir $(objdir-64)
	cd $(objdir-64) && CFLAGS="$(CFLAGS)" CC="gcc -m64" \
		BUILD_CC="gcc" CXX="g++ -m64" \
		$(srcdir)/configure \
		$(CONFARGS) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(build_64_target) \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite --libdir=/usr/lib64

$(objdir-debug)/config.status: patch
	test -d $(objdir-debug) || mkdir $(objdir-debug)
	cd $(objdir-debug) && \
		CFLAGS="$(CFLAGS) -DTRACE" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--enable-overwrite

$(wobjdir)/config.status: patch
	test -d $(wobjdir) || mkdir $(wobjdir)
	cd $(wobjdir) && CFLAGS="$(CFLAGS)" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec

$(wobjdir-debug)/config.status: patch
	test -d $(wobjdir-debug) || mkdir $(wobjdir-debug)
	cd $(wobjdir-debug) && \
		CFLAGS="$(CFLAGS) -DTRACE" $(srcdir)/configure \
		$(CONFARGS) \
		--with-install-prefix=$(fulltempdir) \
		--disable-overwrite --enable-widec

build: build-normal build-wide build-debug build-wide-debug build-static $(build_64)

build-normal: $(objdir)/config.status
	# For ia64...
	cd $(objdir)/c++ \
	  && cat Makefile | sed 's, -c ../c++/demo.cc, -O0 -c ../c++/demo.cc,' > Makefile.tem \
	  && mv -f Makefile.tem Makefile
	cd $(objdir) && $(MAKE)
	touch $@

build-static: $(objdir-static)/config.status
	cd $(objdir-static) && $(MAKE)
	touch $@

build-64: $(objdir-64)/config.status
	# For ia64...
	cd $(objdir-64)/c++ \
	  && cat Makefile | sed 's, -c ../c++/demo.cc, -O0 -c ../c++/demo.cc,' > Makefile.tem \
	  && mv -f Makefile.tem Makefile
	cd $(objdir-64) && $(MAKE)
	touch $@

build-debug: $(objdir-debug)/config.status
	# For ia64...
	cd $(objdir-debug)/c++ \
	  && cat Makefile | sed 's, -c ../c++/demo.cc, -O0 -c ../c++/demo.cc,' > Makefile.tem \
	  && mv -f Makefile.tem Makefile
	cd $(objdir-debug) && $(MAKE)
	touch $@

build-wide: $(wobjdir)/config.status
	# For ia64...
	cd $(wobjdir)/c++ \
	  && cat Makefile | sed 's, -c ../c++/demo.cc, -O0 -c ../c++/demo.cc,' > Makefile.tem \
	  && mv -f Makefile.tem Makefile
	cd $(wobjdir) && $(MAKE)
	touch $@

build-wide-debug: $(wobjdir-debug)/config.status
	# For ia64...
	cd $(wobjdir-debug)/c++ \
	  && cat Makefile | sed 's, -c ../c++/demo.cc, -O0 -c ../c++/demo.cc,' > Makefile.tem \
	  && mv -f Makefile.tem Makefile
	cd $(wobjdir-debug) && $(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	-env QUILT_PATCHES=$(CURDIR)/debian/patches quilt pop -a
	rm -f build install
	rm -rf $(objdir)
	rm -rf $(objdir-64)
	rm -rf $(wobjdir)
	rm -rf $(objdir-debug)
	rm -rf $(wobjdir-debug)
	rm -f debian/shlibs.local
	dh_clean

install: DH_OPTIONS=
install: build
	# Generate proper debhelper files for so-named packages
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -s
	dh_installdirs -i

ifneq ($(build_64),)
	$(MAKE) -C $(objdir-64) install.libs
	test -d $(tempdir)/lib64 || mkdir $(tempdir)/lib64
	mv $(tempdir)/usr/lib64/libncurses.so.$(vername) $(tempdir)/lib64/
	mv $(tempdir)/usr/lib64/libncurses.so.$(soname) $(tempdir)/lib64/
endif

	# Install into our base directory 
	GZIP='-9' $(MAKE) -C $(objdir-debug) install
	$(MAKE) -C $(wobjdir-debug) install.libs

	# Copy libraries to the debug directory
	test -d $(tempdir)/usr/lib/debug || mkdir $(tempdir)/usr/lib/debug
	cp -a $(tempdir)/usr/lib/*.so.* $(tempdir)/usr/lib/debug

	# Rename static debug libraries.
	for lib in $(tempdir)/usr/lib/*.a; do \
	  mv $$lib `echo $$lib | sed 's/\.a$$/_g.a/'`; \
	done

	# Install non -DTRACE versions.
	$(MAKE) -C $(objdir) install.libs
	$(MAKE) -C $(wobjdir) install.libs

	# Compile this before moving files into their packages.
	for ti in xterm hurd rxvt screen; do \
		TERMINFO=$(fulltempdir)/usr/share/terminfo \
		$(objdir-static)/progs/tic debian/$$ti.ti; \
	done

	# We keep the libncurses shared library in /lib.
	test -d $(tempdir)/lib || mkdir $(tempdir)/lib
	mv $(tempdir)/usr/lib/libncurses.so.$(vername) $(tempdir)/lib/
	mv $(tempdir)/usr/lib/libncurses.so.$(soname) $(tempdir)/lib/

	dh_movefiles -s
	dh_movefiles -i

	# We keep the libncurses shared library in /lib.
	ln -sf /lib/libncurses.so.$(soname) debian/$(package-dev)/usr/lib/libncurses.so
ifneq ($(build_64),)
	ln -sf /lib64/libncurses.so.$(soname) debian/$(package-dev-64)/usr/lib64/libncurses.so
endif

	# By tradition, we provide -lcurses and -ltermcap.
	ln -sf libncurses.so debian/$(package-dev)/usr/lib/libcurses.so
	ln -sf libncurses.so debian/$(package-dev)/usr/lib/libtermcap.so
	ln -sf libncurses.a debian/$(package-dev)/usr/lib/libtermcap.a
	ln -sf libncurses.a debian/$(package-dev)/usr/lib/libcurses.a

	# Move terminfo entries for ncurses-base to /etc
	mv debian/$(package-base)/usr/share/terminfo debian/$(package-base)/etc

	# Break hard links.
	cp $(workdir)/debian/$(package-base)/etc/terminfo/r/rxvt-m \
	   $(workdir)/debian/$(package-base)/etc/terminfo/r/rxvt-foo
	rm -f $(workdir)/debian/$(package-base)/etc/terminfo/r/rxvt-m
	mv $(workdir)/debian/$(package-base)/etc/terminfo/r/rxvt-foo \
	   $(workdir)/debian/$(package-base)/etc/terminfo/r/rxvt-m

	cp $(workdir)/debian/$(package-base)/etc/terminfo/E/Eterm-color \
	   $(workdir)/debian/$(package-base)/etc/terminfo/E/Eterm-color-foo
	rm -f $(workdir)/debian/$(package-base)/etc/terminfo/E/Eterm-color
	mv $(workdir)/debian/$(package-base)/etc/terminfo/E/Eterm-color-foo \
	   $(workdir)/debian/$(package-base)/etc/terminfo/E/Eterm-color

	mkdir debian/$(package-base)/usr/share/terminfo
	for f in $(etcterminfodirs); do \
		mkdir debian/$(package-base)/usr/share/terminfo/$$f; \
	done
	for f in $(etcterminfofiles); do \
		ln -sf /etc/terminfo/$$f debian/$(package-base)/usr/share/terminfo/$$f; \
	done

	touch $@

binary:	binary-arch binary-indep

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -s -N$(package-bin) \
		-N$(package-dbg) -N$(package-dbgw) \
		debian/FAQ
	dh_installchangelogs -s -N$(package-bin) \
		-N$(package-dbg) -N$(package-dbgw) \
		NEWS

	mkdir -p debian/$(package-bin)/usr/share/doc
	mkdir -p debian/$(package-dbg)/usr/share/doc
	mkdir -p debian/$(package-dbgw)/usr/share/doc
	ln -s $(package-lib) \
		debian/$(package-bin)/usr/share/doc/$(package-bin)
	ln -s $(package-dev) \
		debian/$(package-dbg)/usr/share/doc/$(package-dbg)
	ln -s $(package-devw) \
		debian/$(package-dbgw)/usr/share/doc/$(package-dbgw)
ifneq ($(build_64),)
	ln -s $(package-dev) \
		debian/$(package-dbgw)/usr/share/doc/$(package-dev-64)
	ln -s $(package-lib) \
		debian/$(package-dbgw)/usr/share/doc/$(package-lib-64)
endif

	dh_strip -s -X$(package-dbg) -X$(package-dbgw)
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -p$(package-lib) -V "$(package-lib) $(sodepver)"
	dh_makeshlibs -p$(package-libw) -V "$(package-libw) $(sodepver)"
ifneq ($(build_64),)
	dh_makeshlibs -p$(package-lib-64) -V "$(package-lib-64) $(sodepver)"
endif
	dh_shlibdeps -p$(package-lib) -p$(package-libw) \
		-ldebian/$(package-lib)/lib:debian/$(package-lib)/usr/lib:debian/$(package-libw)/usr/lib \
		-- -L$(shell pwd)/debian/shlibs.dummy

ifneq ($(build_64),)
	# Unfortunately, dh_shlibdeps runs ldd, and 64-bit ldd doesn't run on
	# a 31-bit S/390 build daemon, so I can't always use dh_shlibdeps.
	# So this is allowed to fail.
	-dh_shlibdeps -p$(package-lib-64) \
		-ldebian/$(package-lib-64)/lib64:debian/$(package-lib-64)/usr/lib64 \
		-- -L$(shell pwd)/debian/shlibs.dummy
endif

	# We don't ship any binaries linked against the 64-bit libraries, so
	# don't add them to shlibs.  They have the same sonames, so they
	# override the 32-bit versions, messing up the dependencies from
	# ncurses-bin.
	cat debian/$(package-lib)/DEBIAN/shlibs \
	  debian/$(package-libw)/DEBIAN/shlibs > debian/shlibs.local

	dh_shlibdeps -s -N$(package-lib) -N$(package-libw) -N$(package-lib-64)\
		-ldebian/$(package-lib)/lib:debian/$(package-lib)/usr/lib:debian/$(package-libw)/usr/lib

	dh_gencontrol -s
	dh_installdeb -s
	dh_builddeb -s

binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot

	# Do not install documentation, just symlinks.
	#	dh_installdocs
	#	dh_installchangelogs NEWS
	mkdir -p debian/$(package-base)/usr/share/doc
	mkdir -p debian/$(package-term)/usr/share/doc
	ln -s $(package-lib) \
		debian/$(package-base)/usr/share/doc/$(package-base)
	ln -s $(package-lib) \
		debian/$(package-term)/usr/share/doc/$(package-term)

	dh_compress -i
	dh_fixperms -i
	dh_gencontrol -i
	dh_installdeb -i
	dh_builddeb -i

# Below here is fairly generic really

checkroot:
	test root = "`whoami`"

.PHONY: binary diff clean checkroot

